<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="维修内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="维修内容"></el-input>
      </el-form-item>
      <el-form-item label="接待人" prop="receiver">
        <el-input v-model="dataForm.receiver" placeholder="接待人"></el-input>
      </el-form-item>
      <el-form-item label="派发时间" prop="sendtime">
        <el-date-picker
          v-model="dataForm.sendtime"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions1"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="维修师" prop="maintainer">
        <el-input v-model="dataForm.maintainer" placeholder="维修师"></el-input>
      </el-form-item>
      <el-form-item label="所属公司" prop="company">
        <el-input v-model="dataForm.company" placeholder="所属公司"></el-input>
      </el-form-item>
      <el-form-item label="维修结果" prop="result">
        <el-input v-model="dataForm.result" placeholder="维修结果"></el-input>
      </el-form-item>
      <el-form-item label="回访人" prop="revisiter">
        <el-input v-model="dataForm.revisiter" placeholder="回访人"></el-input>
      </el-form-item>
      <el-form-item label="回访结果" prop="revisitresult">
        <el-input v-model="dataForm.revisitresult" placeholder="回访结果"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() < Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          },
          {
            text: "明天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24);
              picker.$emit("pick", date);
            }
          },
          {
            text: "一周后",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            }
          }
        ]
      },
      visible: false,
      dataForm: {
        fcId: 0,
        orId: "",
        phone: "",
        content: "",
        receiver: "",
        sendtime: "",
        maintainer: "",
        company: "",
        result: "",
        revisiter: "",
        revisitresult: "",
        state: "",
        createtime: "",
        updatetime: ""
      },
      dataRule: {
        orId: [
          { required: true, message: "居住信息编号不能为空", trigger: "blur" }
        ],
        phone: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("手机不能为空"));
              }

              if (!Number.isInteger(value)) {
                callback(new Error("请输入数字值"));
              } else {
                if (!/^1[0-9]{10}$/.test(value)) {
                  callback(new Error("输入正确手机号"));
                } else {
                  callback();
                }
              }
            },
            trigger: "blur"
          }
        ],
        content: [
          { required: true, message: "维修内容不能为空", trigger: "blur" }
        ],
        receiver: [
          { required: true, message: "接待人不能为空", trigger: "blur" }
        ],
        sendtime: [
          { required: true, message: "派发时间不能为空", trigger: "blur" }
        ],
        maintainer: [
          { required: true, message: "维修师不能为空", trigger: "blur" }
        ],
        company: [
          { required: true, message: "所属公司不能为空", trigger: "blur" }
        ],
        result: [
          { required: true, message: "维修结果不能为空", trigger: "blur" }
        ],
        revisiter: [
          { required: true, message: "回访人不能为空", trigger: "blur" }
        ],
        revisitresult: [
          { required: true, message: "回访结果不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.fcId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.fcId) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/faultrecord/info/${this.dataForm.fcId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.orId = data.faultRecord.orId;
              this.dataForm.phone = data.faultRecord.phone;
              this.dataForm.content = data.faultRecord.content;
              this.dataForm.receiver = data.faultRecord.receiver;
              this.dataForm.sendtime = data.faultRecord.sendtime;
              this.dataForm.maintainer = data.faultRecord.maintainer;
              this.dataForm.company = data.faultRecord.company;
              this.dataForm.result = data.faultRecord.result;
              this.dataForm.revisiter = data.faultRecord.revisiter;
              this.dataForm.revisitresult = data.faultRecord.revisitresult;
              this.dataForm.state = data.faultRecord.state;
              this.dataForm.createtime = data.faultRecord.createtime;
              this.dataForm.updatetime = data.faultRecord.updatetime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/faultrecord/${!this.dataForm.fcId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              fcId: this.dataForm.fcId || undefined,
              orId: this.dataForm.orId,
              phone: this.dataForm.phone,
              content: this.dataForm.content,
              receiver: this.dataForm.receiver,
              sendtime: this.dataForm.sendtime,
              maintainer: this.dataForm.maintainer,
              company: this.dataForm.company,
              result: this.dataForm.result,
              revisiter: this.dataForm.revisiter,
              revisitresult: this.dataForm.revisitresult,
              state: this.dataForm.state,
              createtime: this.dataForm.createtime,
              updatetime: this.dataForm.updatetime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
